package com.origin.chaos.framework.core;

import com.origin.chaos.common.exception.OutPutMsgException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 画布状态机，保存所有画布
 *
 * @author: tao.ding
 * @date 2021-07-16
 */
@Component
public class CanvasMachine {
    /**
     * 存放所有画布的map
     */
    private Map<Long, PixelCanvas> canvasMap = new HashMap<>();

    public void putCanvas(PixelCanvas pixelCanvas) {
        if (this.containsCanvas(pixelCanvas.getId())) {
            throw new OutPutMsgException("画布已存在");
        }
        this.canvasMap.put(pixelCanvas.getId(), pixelCanvas);
    }

    public boolean containsCanvas(Long id){
        return this.canvasMap.containsKey(id);
    }

    public PixelCanvas getCanvas(Long id){
        if (!this.containsCanvas(id)){
            throw new OutPutMsgException("画布不存在");
        }
        return this.canvasMap.get(id);
    }
}
